import Layout from '@/layout';
// 由于懒加载页面太多的话会造成webpack热更新太慢，所以开发环境不使用懒加载，只有生产环境使用懒加载
// const _import = require('@/utils/util.import');
export const filterAsyncRouter = (routers) => {
  // 遍历后台传来的路由字符串，转换为组件对象
  let filterRoutes = [];
  routers.forEach((item) => {
    if (item.url === '/home') return;
    let route = {
      component: '',
      path: item.url,
      meta: {
        title: item.name,
        icon: item.icon,
      },
    };
    if (item.url) {
      if (item.children && item.children.length) {
        // Layout组件特殊处理
        route.component = Layout;
        let children = [];
        item.children.forEach((val) => {
          let childrenRoute = {
            component: loadView(val.url),
            path: val.url,
            meta: {
              title: val.name,
              icon: val.icon,
            },
          };
          children.push(childrenRoute);
        });
        route['children'] = children;
      } else {
        const component = item.url;
        route.component = loadView(component);
      }
    }
    filterRoutes.push(route);
  });
  return filterRoutes;
};

export const loadView = (view) => {
  // 路由懒加载
  return () => import(`@/views/${view}`);
};
